﻿<!DOCTYPE html>

<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title>WebSocketTest</title>
</head>
<body>
    <button type="button" onclick="openWebSocket()">打开</button>
    <input id="inputText" type="text" />
    <button type="button" onclick="sendMessage()">发送</button>
    <button type="button" onclick="closeWebSocket()">关闭</button>
    <button type="button" onclick="sendCommandTest01()">发送命令Test01</button>
    <button type="button" onclick="sendCommandTest02()">发送命令Test02</button>
    <script>
        var websocketUrl = "ws://192.168.2.2:8800/websocket";
        var inputText = document.getElementById("inputText");
        var websocketClient;
        function sendMessage() {
            if (!websocketClient) return;
            websocketClient.send(inputText.value);
        }
        function sendCommand(command) {
            if (!websocketClient) return;
            var commandJson = JSON.stringify(command);
            websocketClient.send(commandJson);
        }
        function sendCommandTest01() {
            var command = {
                Command: "Test01Command",
                Name: "Materal",
                Age: 26
            };
            sendCommand(command);
        }
        function sendCommandTest02() {
            var command = {
                Command: "Test02Command",
                Author: "Materal",
                Content: inputText.value
            };
            sendCommand(command);
        }
        function closeWebSocket() {
            if (!websocketClient) return;
            websocketClient.close();
        }
        function openWebSocket() {
            closeWebSocket();
            websocketClient = new WebSocket(websocketUrl);
            websocketClient.onopen = event => {
                console.log("链接已打开", event);
            };
            websocketClient.onmessage = event => {
                console.log("收到消息", event);
            };
            websocketClient.onclose = event => {
                console.log("链接已关闭", event);
            };
            websocketClient.onerror = event => {
                console.log("WebSocket出错", event);
            };
        }
    </script>
</body>
</html>